package com.hanhai.zrb.mobile.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hanhai.common.annotation.LoginNeedless;
import com.hanhai.common.web.controller.BaseController;
import com.hanhai.zrb.api.biz.service.PhotoService;
import com.hanhai.zrb.api.biz.service.ProjectService;
import com.hanhai.zrb.api.biz.service.UserInfoService;
import com.hanhai.zrb.api.biz.service.UserService;
import com.hanhai.zrb.api.biz.service.UserStatisticDetailService;
import com.hanhai.zrb.mobile.result.BaseApiResult;
import com.hanhai.zrb.mobile.util.FrontLoginUtil;
import com.hanhai.zrb.model.common.enums.StatusEnums;
import com.hanhai.zrb.model.photo.Photo;
import com.hanhai.zrb.model.project.Project;
import com.hanhai.zrb.model.project.ProjectDetail;
import com.hanhai.zrb.model.user.User;
import com.hanhai.zrb.model.user.UserBase;
import com.hanhai.zrb.model.user.enums.UserInfoName;
import com.hanhai.zrb.model.user.vo.UserInfoVO;

@RequestMapping("/projectApi")
@Controller
public class ProjectController extends BaseController{
	
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private ProjectService projectService;
	
	//@Autowired
	//private GetProjectInfoEmailService getProjectInfoEmailService;
	
	@Autowired
	private  UserStatisticDetailService userStatisticDetailService;
	
	@Resource
	private PhotoService photoService;
	
	@Resource
	private UserService userService;
	
	@Resource
	private UserInfoService userInfoService;
	
	@RequestMapping(value="getMyProjectList")
	@ResponseBody
	public BaseApiResult getMyProjectList(Date lastPushTime,int rows){
		
		
		List<Project> proLs = projectService.listForMobile(FrontLoginUtil.getCurrentUser().getUid(),lastPushTime, rows);
		
		BaseApiResult ret = new BaseApiResult();
		ret.setData(proLs);
		
		return ret;
		
	}
	
	@RequestMapping(value="getProjectList")
	@ResponseBody
	@LoginNeedless
	public BaseApiResult getProjectList(Date lastPushTime,int rows){
		
		List<Project> proLs = projectService.listForMobile(lastPushTime, rows);
		
		BaseApiResult ret = new BaseApiResult();
		ret.setData(proLs);
		
		return ret;
		
	}
	
	@RequestMapping(value="getUserProjectList")
	@ResponseBody
	public BaseApiResult getMyProjectList(Long userId,Date lastPushTime,int limit){
		
		List<Project> proLs = projectService.listForMobile(userId,lastPushTime, limit);
		
		BaseApiResult ret = new BaseApiResult();
		ret.setData(proLs);
		
		return ret;
		
	}
	
	@RequestMapping(value="searchProList")
	@LoginNeedless
	@ResponseBody
	public BaseApiResult searchProList(@RequestParam(value="lastPushTime", required=false) Date lastPushTime,@RequestParam(value="postcode", required=false) String postcode,@RequestParam(value="industrys", required=false) String industrys,@RequestParam(value="mode", required=false) String mode,int rows){
		List<String> postcodeList= new ArrayList<String>();
		List<Integer> industrysList= new ArrayList<Integer>();
		if(StringUtils.isNoneBlank(postcode)){
			String[] cityStrs = postcode.split(",");
			for (int i = 0; i < cityStrs.length; i++) {
				postcodeList.add(cityStrs[i]);
			}
		}
		
		if(StringUtils.isNoneBlank(industrys)){
			String[] industryStr = industrys.split(",");
			for (int i = 0; i < industryStr.length; i++) {
				if(StringUtils.isNumeric(industryStr[i]) ){
					if("97".equals(industryStr[i])){
						industrysList = null;
						break;
					}else{
						industrysList.add(Integer.valueOf(industryStr[i]));
					}
					
				}
			}
		}
	//	Date date = (null==lastPushTime?null:DateUtil.getStringToDate(DateUtil.FULL_ST_FORMAT,DateUtil.getDate(lastPushTime)));
		List<Project> proLs = projectService.listForIndustryAndPostcode(lastPushTime,industrysList,postcodeList,mode, rows);
		
		BaseApiResult ret = new BaseApiResult();
		ret.setData(proLs);
		
		return ret;
		
	}
	
	
	
	@RequestMapping(value="getProjectDetail")
	@LoginNeedless
	public String getProjectDetail(long id,	HttpServletRequest request){
		ProjectDetail projectDetail = projectService.qryProjectDetailById(id);
		if(null != projectDetail && (short)203 == projectDetail.getProjectStatus()){
			request.setAttribute("projectDetail", projectDetail);

			return "project/projectDetails";
		}
	
		return "message";
	}
	
	@RequestMapping(value="getProjectDetailPreView")
	@LoginNeedless
	public String getProjectDetailPreView(long id,	HttpServletRequest request){
		ProjectDetail projectDetail = projectService.qryProjectDetailById(id);
		List<Photo> photoList = photoService.getPhotoListByProjectId(id);
		if( !photoList.isEmpty()){
			request.setAttribute("photoCount", photoList.size());
		}
		request.setAttribute("projectDetail", projectDetail);

		return "project/projectDetails";
	}
	
	@LoginNeedless
	@RequestMapping("getPhotoList")
	public void getPhotoList(HttpServletResponse response, Model model,@RequestParam Long id) {
		List<Photo> photoList = photoService.getPhotoListByProjectId(id);
		super.returnObjectJsonString(response, photoList);
	}
	
	/*@RequestMapping(value="saveProjectEmail")
	@ResponseBody
	@LoginNeedless
	public BaseApiResult saveProjectEmail(int projectId,String projectName,String email){
		BaseApiResult ret = new BaseApiResult();
		
		ProjectEmail projectEmail = new ProjectEmail();
		projectEmail.setCreateTime(new java.util.Date());
		projectEmail.setStatus(1);//未发送
		projectEmail.setProjectId(projectId);
		projectEmail.setProjectName(projectName);
		projectEmail.setEmail(email);
		
		getProjectInfoEmailService.insert(projectEmail);
		
		ret.setMsg("保存成功");
		return ret;
		
	}*/
	
	@RequestMapping("/getProjectUserInfo")
	@ResponseBody
	@LoginNeedless
	public BaseApiResult getProjectUserInfo(Long projectId){
		BaseApiResult ret = new BaseApiResult();
		
		
		Project project = projectService.selectByPrimaryKey(projectId);
		
		if(project.getProjectStatus()==StatusEnums.RELEASE.getCode().shortValue()){
			String userIdStr = project.getPusher();
			if(StringUtils.isNoneBlank(userIdStr)&&StringUtils.isNumeric(userIdStr)){
				User user = userService.findById(Long.valueOf(userIdStr));
				if(user !=null){
					UserBase userBase = new UserBase();
					userBase.setUid(user.getUid());
					userBase.setIndustryId(user.getIndustryId());
					userBase.setIndustryName(user.getIndustry());
					userBase.setRealName(user.getRealname());
					userBase.setUmobile(user.getUmobile());
					userBase.setOrg(user.getOrg());
					userBase.setAvatar(user.getAvatar());

					//获取用户职位
					List<UserInfoName> queryNameList = new ArrayList<UserInfoName>();
					queryNameList.add(UserInfoName.POSITION);
					UserInfoVO userInfoVo = userInfoService.findByNameEnums(
							Long.toString(user.getUid()), queryNameList);
					Map<String, String> infoMap = userInfoVo.getInfoMap();
					String position = infoMap.get("POSITION");
					userBase.setPosition(position);
					ret.setData(userBase);
					return ret;
				}
				
			}
		}
		
		ret.setRes(0);
		ret.setMsg("无效数据，请刷新再试");
		return ret;
		
	}

}
